<?php

class CategoryController extends AdminBase{

    public function actionIndex()
    {

        $arrData = CategoryModel::getListTreeArr();
        $this->assign('count', count($arrData));
        $this->assign('model', CategoryModel::model());
        $this->assign('data', $arrData);
        $this->render('index');  
    }

    public function actionDelete($id)
    {
        if(CategoryModel::deleteCategoryByPk($id)){
            $this->success('删除成功');     
        }
        $this->error('删除失败');  
    }

    public function actionCreate(){
        if(IS_POST && !empty($_POST['CategoryModel']['TagListIDs'])){
            $_POST['CategoryModel']['TagListIDs'] = implode(',', $_POST['CategoryModel']['TagListIDs']);
        }        
        $objModel = new CategoryModel('admin');
        $intCatID = Yii::app()->request->getParam('id');
        if($intCatID){
            $objModel->ParentID = $intCatID;
        }
        if(isset($_POST['CategoryModel']))
        {
            //赋值
            $objModel->attributes = $_POST['CategoryModel'];
        
            if($objModel->save())
            {           
                $this->success('添加成功');     
            }else
            {
                $error = $objModel->getOneError();
                $this->error($error ? $error : '添加失败');         
            }
        }
        $this->render('create', array('model' => $objModel));   
    }
    public function actionUpdate($id){
        $objModel = $this->loadModel($id);
        if(IS_POST && !empty($_POST['CategoryModel']['TagListIDs'])){
            $_POST['CategoryModel']['TagListIDs'] = implode(',', $_POST['CategoryModel']['TagListIDs']);
        }else{
            $objModel->TagListIDs = explode(',', $objModel->TagListIDs);
        }
        $this->baseUpdate($objModel);
    }

    public function loadModel($id)
    {
        $model=CategoryModel::model()->findByPk($id);
        if($model===null)
            $this->error('找不到记录');
        return $model;
    }    
}